
/*
This file is part of MJIN2.

Copyright (C) 2012-2013 Opensource Game Studio

This software is provided 'as-is', without any express or implied
warranty.  In no event will the authors be held liable for any damages
arising from the use of this software.

Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented; you must not
   claim that you wrote the original software. If you use this software
   in a product, an acknowledgment in the product documentation would be
   appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
   misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/

#include <mjin2/Light.h>
#include <mjin2/Scene.h>
#include <mjin2/SceneNode.h>
#include <mjin2/priv/Light.h>
#include <mjin2/priv/Scene.h>
#include <mjin2/priv/SceneNode.h>

#include <osg/Group>
#include <osg/PositionAttitudeTransform>
#include <osgShadow/SoftShadowMap>

namespace mjin2
{
namespace priv
{

Scene::Scene() throw () : mMaskCastShadow(0x2)
{
    mScene = new osgShadow::ShadowedScene;
    mScene->setCastsShadowTraversalMask(mMaskCastShadow);
    createLight();
    recreateShadows();
}

Scene::~Scene() throw ()
{
    clear();
    destroyAllLights();
}

void Scene::addChild(mjin2::SceneNode *n) throw ()
{
    mScene->addChild(n->mD->mNode);
    mChildren.push_back(n);
}

mjin2::SceneNode *Scene::child(const String &name) throw ()
{
    for (auto it = mChildren.begin(); it != mChildren.end(); ++it)
        if ((*it)->name() == name)
            return *it;
    MJIN2_EXCEPTION("Invalid scene node",
                    MJIN2_STR("SceneNode '%s' of Scene '%llu' cannot be "
                              "found", name.c_str(), this));
}

void Scene::clear() throw ()
{
    for (auto it = mChildren.begin(); it != mChildren.end(); ++it)
    {
        mScene->removeChild((*it)->mD->mNode);
        delete *it;
    }
    mChildren.clear();
}

mjin2::Light *Scene::light() throw ()
{
    return mLights[0];
}

void Scene::recreateShadows() throw ()
{
    osg::ref_ptr<osgShadow::SoftShadowMap> sm = new osgShadow::SoftShadowMap;
    const u16 shadowSize = 4096;
    sm->setTextureSize(osg::Vec2s(shadowSize, shadowSize));
    sm->setTextureUnit(1);
    sm->setJitteringScale(16);
    /*
    osg::ref_ptr<osgShadow::ShadowMap> sm = new osgShadow::ShadowMap;
    const u16 shadowSize = 4096;
    sm->setTextureSize(osg::Vec2s(shadowSize, shadowSize));
    sm->setTextureUnit(1);
    */
    mScene->setShadowTechnique(sm.get());
}

void Scene::removeChild(mjin2::SceneNode *n) throw (Exception)
{
    for (auto it = mChildren.begin(); it != mChildren.end(); ++it)
        if (*it == n)
        {
            mScene->removeChild((*it)->mD->mNode);
            mChildren.erase(it);
            return;
        }
    MJIN2_EXCEPTION("Invalid scene node",
                    MJIN2_STR("SceneNode '%llu' of Scene '%llu' cannot be "
                              "destroyed, because it is not found", n, this));
}

osg::ref_ptr<osgShadow::ShadowedScene> Scene::scene() throw ()
{
    return mScene;
}

void Scene::setChildCastShadow(mjin2::SceneNode *n, bool state) throw ()
{
    n->mD->mNode->setNodeMask(state ? mMaskCastShadow : 0x1);
}

mjin2::Light *Scene::createLight() throw ()
{

    mjin2::Light *l = new mjin2::Light(new priv::Light(mScene));
    mLights.push_back(l);
    return l;
}

void Scene::destroyAllLights() throw ()
{
    for (auto it = mLights.begin(); it != mLights.end(); ++it)
        delete *it;
    mLights.clear();
}

void Scene::destroyLight(mjin2::Light *l) throw ()
{
    for (auto it = mLights.begin(); it != mLights.end(); ++it)
        if (*it == l)
        {
            delete *it;
            mLights.erase(it);
            return;
        }
    MJIN2_EXCEPTION("Invalid scene light",
                    MJIN2_STR("Light '%llu' of Scene '%llu' cannot be "
                              "destroyed, because it is not found", l, this));
}

} // namespace priv
} // namespace mjin2

